package com.share.example.selenium.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.share.common.common.Page;
import com.share.example.selenium.mapper.PromoteDetailMapper;
import com.share.example.selenium.model.amazon.seller.ClosePromoteForm;
import com.share.example.selenium.model.amazon.seller.PromoteForm;
import com.share.example.selenium.model.amazon.seller.PromoteTask;
import com.share.example.selenium.model.db.PromoteDetail;
import com.share.example.selenium.model.vo.PromoteDetailPageReq;
import com.share.example.selenium.service.IBaseConfService;
import com.share.example.selenium.service.IPromoteDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-10-18
 */
@Service
public class PromoteDetailServiceImpl extends ServiceImpl<PromoteDetailMapper, PromoteDetail> implements IPromoteDetailService {
    @Resource
    private PromoteDetailMapper promoteDetailMapper;
    @Autowired
    private IBaseConfService baseConfService;

    //创建活动添加日志记录
    @Override
    public int insertCreatePromotion(String batchId, PromoteTask task, PromoteForm promotion) {
        PromoteDetail record = new PromoteDetail();
        record.setCreateTime(new Date());
        record.setBatchId(batchId);
        record.setUserId(baseConfService.getUserCode());

        record.setBenift(task.getBenefit());
        record.setStartDate(task.getStartDate());
        record.setStartTime(task.getStartTime());
        record.setAsin(task.getAsin());
        record.setCreateTime(new Date());
        record.setStatus("fail");
        record.setActionName("createPromottion");
        if (promotion != null) {
            record.setStatus("success");
            record.setBenift(promotion.getBenift());
            record.setLink(promotion.getPromotionLink());
            record.setGoodsName(promotion.getGoodsName());
            record.setPromoteName(promotion.getPromotionName());
            record.setPromoteCode(promotion.getPromotionCode());
            record.setAsin(promotion.getAsin());

            record.setStartTime(promotion.getStartTime());
            record.setStartDate(promotion.getStartDate());
            record.setEndTime(promotion.getEndtTime());
            record.setEndDate(promotion.getEndDate());
            record.setCouponCode(promotion.getCouponCode());
        }

        return promoteDetailMapper.insert(record);
    }

    //关闭活动记录
    @Override
    public int insertClosePromotion(String batchId, ClosePromoteForm form, boolean re, String errMsg) {
        PromoteDetail record = new PromoteDetail();
        record.setCreateTime(new Date());
        record.setBatchId(batchId);
        record.setOtherMsg(errMsg);
        record.setActionName("closePromottion");
        record.setUserId(baseConfService.getUserCode());

        if (form == null) {
            form = new ClosePromoteForm();
        }

        record.setOrderNum(form.getOrderNum());
        record.setLink(form.getLink());
        record.setTrackName(form.getTrackName());
        record.setPromoteName(form.getPromotionName());

        if (StringUtils.isNotBlank(form.getStartDateTime())) {
            String[] s = form.getStartDateTime().trim().split(" ");
            if (s.length == 3) {
                record.setStartDate(s[0]);
                record.setStartTime(s[1]);
            }
        }

        if (StringUtils.isNotBlank(form.getEndDateTime())) {
            String[] s = form.getEndDateTime().trim().split(" ");
            if (s.length == 3) {
                record.setEndDate(s[0]);
                record.setEndTime(s[1]);
            }
        }

        if (re) {
            record.setStatus("success");
        } else {
            record.setStatus("fail");
        }


        return promoteDetailMapper.insert(record);
    }

    @Override
    public Page pageList(PromoteDetailPageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        LambdaQueryWrapper<PromoteDetail> queryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(req.getActionName())) {
            queryWrapper.like(PromoteDetail::getActionName, req.getActionName());
        }
        if (StringUtils.isNotBlank(req.getAsin())) {
            queryWrapper.like(PromoteDetail::getAsin, req.getAsin());
        }
        if (StringUtils.isNotBlank(req.getBatchId())) {
            queryWrapper.like(PromoteDetail::getBatchId, req.getBatchId());
        }
        if (StringUtils.isNotBlank(req.getPromoteCode())) {
            queryWrapper.like(PromoteDetail::getPromoteCode, req.getPromoteCode());
        }
        if (StringUtils.isNotBlank(req.getLink())) {
            queryWrapper.like(PromoteDetail::getLink, req.getLink());
        }
        if (StringUtils.isNotBlank(req.getCouponCode())) {
            queryWrapper.like(PromoteDetail::getCouponCode, req.getCouponCode());
        }
        if (StringUtils.isNotBlank(req.getActionName())) {
            queryWrapper.like(PromoteDetail::getActionName, req.getActionName());
        }
        if (StringUtils.isNotBlank(req.getStatus())) {
            queryWrapper.like(PromoteDetail::getStatus, req.getStatus());
        }
        queryWrapper.orderByDesc(PromoteDetail::getId);
        List<PromoteDetail> list = promoteDetailMapper.selectList(queryWrapper);
        return new Page<>(list, new PageInfo<>(list).getTotal(), req);
    }
}
